#include "ServidorDeVariables.h"

ServidorDeVariables::ServidorDeVariables()
{
	socket = NULL;
	genVar = NULL;
} 

ServidorDeVariables::~ServidorDeVariables()
{
	for(unsigned i=0; i<clientes.size();i++)
		delete clientes[i];
	if (socket)
		delete socket;
	if (genVar)
		delete genVar;
}

bool ServidorDeVariables::inicializar(int port, std::string path)
{printf("%s\n", "se inicializa el server");
	socket = new Socket((Socket::port_type)port);
	genVar = new GeneradorDeVariables("vars.txt");
	return (bool) *socket;
}

void ServidorDeVariables::escucharConexiones()
{printf("%s\n", "el server escucha conexiones");
	bool fin = false;
	Socket* sck;
	ClientHandler* cliente; 
	while(!fin)
	{
		sck = socket->accept();
		if(*sck)
		{
			cliente = new ClientHandler(sck, genVar);
			cliente->run();
			clientes.push_back(cliente);
			// acepta una conexion y sale(para probar)
			fin= true;
		}
	}
	for(unsigned i=0; i<clientes.size();i++)
		clientes[i]->join();
}


